package com.itheima.reggie;

import com.itheima.reggie.config.InitContext;
import com.itheima.reggie.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.BeansException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.io.File;

@Slf4j
@SpringBootApplication
@MapperScan("com.itheima.reggie.mapper")
@ServletComponentScan
@EnableTransactionManagement
public class ReggieApplication implements ApplicationContextAware {

    static {
        System.setProperty("druid.mysql.usePingMethod","false");
    }

    public static void main(String[] args) {
        SpringApplication.run(ReggieApplication.class,args);
        log.info("项目启动成功...");
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        InitContext.getInitContext().setApplicationContext(applicationContext);
        //使用阿里存储图片，不需要初始化路径
       // getImgFile(applicationContext);
    }

    //生产图片保存路径
    private void getImgFile(ApplicationContext applicationContext){
        Environment env = applicationContext.getBean(Environment.class);
        //创建一个目录对象
        File dir = new File(env.getProperty(CommonConstant.IMAGE_PATH));
        //判断当前目录是否存在
        if(!dir.exists()){
            //目录不存在，需要创建
            dir.mkdirs();
        }
    }


}